package com.minelx.sqlstructures.statements.clause;

import java.util.List;
import java.util.function.Function;

import static java.util.stream.Collectors.joining;

public interface Sql {
	Sql EMPTY = Sql.of("");

	String toText();

	default Sql map(Function<String, String> mapper) {
		return () -> mapper.apply(toText());
	}

	static Sql of(String content) {
		return () -> content;
	}

	static Sql empty() {
		return Sql.EMPTY;
	}

	static Sql join(String joiner, List<Sql> allSql) {
		return () -> allSql.stream()
				.map(Sql::toText)
				// FIXME 2021/6/29  wait for me!!!   use Sql.empty or not String.isEmpty
				.filter(sql -> !sql.isEmpty())
				.collect(joining(joiner));
	}

	default Sql prefix(String prefix) {
		String asText = toText();
		if (asText.isEmpty()) {
			return empty();
		}
		return () -> prefix + asText;
	}
}
